﻿using System;

namespace Reactor.Messaging.ServiceBus.Internals
{
    /// <summary>
    /// Encapsulation of a single message transmission task. Responsible for either publishing a message to a subscription topic 
    /// or sending to a MessagingChannel queue.
    /// </summary>
    public class TransmissionTask : TaskBase
    {
        #region Properties

        /// <summary>
        /// Gets or sets the MessagingChannel to which the message is to be sent.
        /// </summary>
        /// <value>The MessagingChannel.</value>
        public override IMessagingChannel MessagingChannel { get; set; }

        /// <summary>
        /// Gets or sets the reply MessagingChannel. That is, the address 
        /// the receiver of this request is to reply to.
        /// </summary>
        /// <value>The reply MessagingChannel.</value>
        public IMessagingChannel ReplyDestination { get; set; }

        /// <summary>
        /// Gets or sets an identifier to uniquely identify a request/reply exchange of messages.
        /// </summary>
        /// <value>The request identifier.</value>
        public Guid RequestIdentifier { get; set; }

        /// <summary>
        /// Gets or sets the message.
        /// </summary>
        /// <value>The message.</value>
        public object Message { get; set; }

        /// <summary>
        /// Gets or sets the workflow instance id.
        /// </summary>
        /// <value>The workflow instance id.</value>
        public Guid WorkflowInstanceId { get; set; }

        #endregion

        /// <summary>
        /// Validates this instance by ensuring the MessagingChannel is of proper type and the Message property is not null.
        /// </summary>
        public override void Validate()
        {
            if(MessagingChannel == null)
                throw new InvalidOperationException("Cannot transmit message to null MessagingChannel.");

            if (!(MessagingChannel is PointToPointChannel || MessagingChannel is PubSubChannel))
                throw new InvalidOperationException("TransmissionTask.MessagingChannel must be either a queue or subscription topic.");

            if(Message == null)
                throw new InvalidOperationException("TransmissionTask.Message cannot be null.");
        }
    }
}